package com.sanley.piss.web.controller;

import com.sanley.piss.model.exceptionUtil.json.JsonResult;
import com.sanley.piss.model.po.Menu;
import com.sanley.piss.model.vo.MenuTree;
import com.sanley.piss.web.service.mapper.IMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Set;

/**
 * Create By  on 2021/3/19.
 * @author 张凯大天才
 */
@Api(value = "资源表controller",tags = "资源表操作接口")
@RestController
@RequestMapping("menu")
public class MenuController {

    @Autowired
    private IMenuService service;

    @ApiOperation(value = "增加一条记录")
    @RequestMapping(value = "insert",method = RequestMethod.PUT)
    public JsonResult insert(@Valid Menu menu) {
        service.insert(menu);
        return JsonResult.success();
    }

    @ApiOperation(value = "根据ID删除多条记录")
    @RequestMapping(value = "/delete/{ids}",method = RequestMethod.DELETE)
    public JsonResult removeByIds(@PathVariable("ids") Integer[] ids) {
        service.removeByIds(ids);
        return JsonResult.success();
    }

    @ApiOperation(value = "根据ID修改一条记录")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public JsonResult update(@Valid Menu menu) {
        service.updateById(menu);
        return JsonResult.success();
    }

    /**
     * 根据当前节点的父级ID 获取 上级所有节点的ID
     * @return
     */
    @ApiOperation(value = "根据当前节点的父级ID 获取 上级所有节点的ID")
    @GetMapping("/getMenIdsByParentId/{parentId}")
    public JsonResult<Set<Integer>> getMenIdsByParentId(@PathVariable("parentId") Integer parentId){
        Set<Integer> integers = service.selectMenuIdsByParentId(parentId);
        return JsonResult.success(integers);
    }

    /**
     * 根据当前菜单ID 获取所有子节点id
     * @return
     */
    @ApiOperation(value = "根据当前菜单ID 获取所有子节点id")
    @GetMapping("getMenuSonIdsByMenId/{menuId}")
    public JsonResult<Set<Integer>> getMenuSonIdsByMenId(@PathVariable("menuId") Integer menuId){
        Set<Integer> integers = service.selectMenuSonIdsByMenuId(menuId);
        return JsonResult.success(integers);
    }

    @ApiOperation(value = "查询资源树")
    @GetMapping("/tree/all")
    public JsonResult<List<MenuTree>> menuTreeAll()
    {
        return JsonResult.success(service.selectAllMenuTree());
    }
    /**
    是否拥有权限
     */
    @ApiOperation(value = "是否拥有权限")
    @GetMapping("/auth/check")
    public JsonResult<Object> authCheck(String code)
    {
        SecurityUtils.getSubject().checkPermission(code);
        return JsonResult.success();
    }

    /**
     * 获取角色 拥有的菜单 id
     * 做角色权限修改回显
     *
     * @param roleId
     * @return
     */
    @ApiOperation(value = " 获取角色 拥有的菜单 id")
    @GetMapping("/getMenuIdsByRoleId/{roleId}")
    public JsonResult<Set<Integer>> getMenuIdByRoleId(@PathVariable("roleId") Integer roleId)
    {
        return JsonResult.success(service.selectMenuIdByRoleId(roleId));
    }
    /**
     * 用户的菜单
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "查询用户的菜单 角色")
    @GetMapping("/userMenuTree")
    public JsonResult<List<MenuTree>> userMenuTree( Integer userId)
    {
        return JsonResult.success(service.selectUserMenuTree(userId));
    }

    /**
     * 查询用户的权限Id
     * @param userId
     * @return
     */
    @ApiOperation(value = "查询用户的权限Id")
    @GetMapping("/userMenuTreeIds/{userId}")
    public JsonResult<List<Integer>> userMenuTreeIds(@PathVariable("userId") Integer userId)
    {
        return JsonResult.success(service.selectUserMenuTreeIds(userId));
    }




}
